package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.COrderStatusEnum;
import cn.huiyunche.base.service.enums.OrderStatusLogOperatorEnum;
import cn.huiyunche.base.service.enums.PaymentStatusEnum;
import cn.huiyunche.base.service.enums.StatusCategoryEnum;
import cn.huiyunche.base.service.interfaces.OrderStatusLogService;
import cn.huiyunche.base.service.interfaces.SOrderService;
import cn.huiyunche.base.service.interfaces.UserService;
import cn.huiyunche.base.service.mappers.SOrderStatusLogMapper;
import cn.huiyunche.base.service.model.SOrder;
import cn.huiyunche.base.service.model.SOrderStatusLog;
import cn.huiyunche.base.service.model.SOrderStatusLogExample;
import cn.huiyunche.base.service.model.SUser;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.base.service.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderStatusLogServiceImpl implements OrderStatusLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderStatusLogServiceImpl.class);

    @Autowired
    private SOrderStatusLogMapper logMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private SOrderService orderService = null;

    @Override
    public int insertSelective(long orderId, StatusCategoryEnum enum1,
                               COrderStatusEnum enum2, boolean isSystem, UserVo user) throws Exception {
        if (orderId == 0) {
            throw new IllegalArgumentException("OrderStatusLogServiceImpl.insertSelective param orderId can not be 0!");
        }
        if (enum1 == null || enum2 == null) {
            throw new IllegalArgumentException("OrderStatusLogServiceImpl.insertSelective param enum2|enum3 can not be null!");
        }
        SOrderStatusLog log = new SOrderStatusLog();
        log.setOrderId(orderId);
        log.setCategory(enum1.getValue());
        log.setStatus(enum2.getValue());
        if (isSystem) {
            log.setOperator(OrderStatusLogOperatorEnum.SYSTEM.getValue());
            log.setUserId((long) 0);
            log.setUserName("system");
        } else {
            if (null == user) {
                user = userService.getCurrentUserVo();
            }
            log.setOperator(OrderStatusLogOperatorEnum.USER.getValue());
            log.setUserId(user.getId());
            log.setUserName(user.getUsername());
        }
        return logMapper.insertSelective(log);
    }

    @Override
    public int insertSelective(long orderId, StatusCategoryEnum enum1,
                               PaymentStatusEnum enum2) {
        if (orderId == 0) {
            throw new IllegalArgumentException("OrderStatusLogServiceImpl.insertSelective param orderId can not be 0!");
        }
        if (enum1 == null || enum2 == null) {
            throw new IllegalArgumentException("OrderStatusLogServiceImpl.insertSelective param enum1|enum2 can not be null!");
        }
        int count = 0;
        try {
            SOrderStatusLog log = new SOrderStatusLog();
            log.setOrderId(orderId);
            log.setOperator(OrderStatusLogOperatorEnum.USER.getValue());
            log.setCategory(enum1.getValue());
            log.setStatus(enum2.getValue());

            Result<SOrder> orderById = this.getOrderService().getOrderById(orderId);
            SUser user = userService.getById(orderById.getData().getUserId());
            log.setUserId(user.getId());
            log.setUserName(user.getPhone());

            count = logMapper.insertSelective(log);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int insertSelective(long orderId, long userId, String userName, StatusCategoryEnum enum1,
                               PaymentStatusEnum enum2) {
        if (orderId == 0) {
            throw new IllegalArgumentException("OrderStatusLogServiceImpl.insertSelective param orderId can not be 0!");
        }
        if (enum1 == null || enum2 == null) {
            throw new IllegalArgumentException("OrderStatusLogServiceImpl.insertSelective param enum1|enum2 can not be null!");
        }
        int count = 0;
        try {
            SOrderStatusLog log = new SOrderStatusLog();
            log.setOrderId(orderId);
            log.setUserId(userId);
            log.setUserName(userName);
            log.setStatus(enum2.getValue());
            log.setOperator(OrderStatusLogOperatorEnum.USER.getValue());
            log.setCategory(enum1.getValue());
            count = logMapper.insertSelective(log);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    public SOrderService getOrderService() {
        return this.orderService;
    }

    @Override
    public int insertSelective(SOrderStatusLog log) {
        return logMapper.insertSelective(log);
    }

    @Override
    public void deleteByCOrderId(long customerOrderId) {
        LOGGER.info("deleteByCOrderId param : {}", customerOrderId);
        if (0 == customerOrderId) {
            LOGGER.error("deleteByCOrderId customerOrderId must not be 0");
            throw new IllegalArgumentException("客户订单id不能是0");
        }

        SOrderStatusLogExample example = new SOrderStatusLogExample();
        example.createCriteria().andOrderIdEqualTo(customerOrderId);
        logMapper.deleteByExample(example);
    }

}
